(function($) {
    $.fn.widget = function(opt) {
        var options = $.extend({}, $.fn.widget.defaults, opt || {});

        var $this = $(this);

        var removeWidget = function() {
            $this.animate({
                opacity: 0
            }, function() {
                $this.slideUp(function() {
                    $(this).remove();
                });
            });
        }

        if (options.removable) {
            $('<a href="#" class="remove">CLOSE</a>').mousedown(function(e) {
                e.stopPropagation();
            }).click(function() {
                if (alertMsg) {
                    alertMsg.confirm("确定删除?", { okCall: function() { removeWidget(); } });
                }
                else {
                    if (confirm('确定删除?')) {
                        removeWidget();
                    }
                }
                return false;
            }).appendTo($(options.headSelector, $this));
        }
        if (options.collapsible) {
            $('<a href="#" class="collapse">COLLAPSE</a>').mousedown(function(e) {
                e.stopPropagation();
            }).toggle(function() {
                $(this).css({ backgroundPosition: '0 -100px' });
                $this.find(options.contentSelector).hide();
                return false;
            }, function() {
                $(this).css({ backgroundPosition: '' })
                $this.find(options.contentSelector).show();
                return false;
            }).prependTo($(options.headSelector, $this));
        }
    };

    $.fn.widget.defaults = {
        headSelector: '.widget-head',
        contentSelector: '.widget-content',
        collapsible: true,
        removable: true
    };
})(jQuery);

